package com.xunshangwang.pinhaohuo.ui.activities.address;

import com.xunshangwang.pinhaohuo.entity.Address;
import com.xunshangwang.pinhaohuo.http.HttpTool;
import com.xunshangwang.pinhaohuo.http.JsonHttpHandler;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;
import tool.library.ToastTool;

/**
 * Created by xzp on 16/2/16.
 */
public class EditAddressActivity extends BaseAddressActivity{
    @Override
    void initAddress() {
        mAddress= (Address) getIntent().getExtras().getSerializable("address");
        mAddressText.setText(mAddress.getProvince()+"-"+mAddress.getCity()+"-"+mAddress.getDistrict());
        mLongAddressEdit.setText(mAddress.getAddress());
        mReceiveNameEdit.setText(mAddress.getReceive_name());
        mPhoneEdit.setText(mAddress.getMobile());
    }

    @Override
    void requestSave() {
        JSONObject object=new JSONObject();
        try {
            object.put("province",mAddress.getProvince_id());
            object.put("city",mAddress.getCity_id());
            object.put("district",mAddress.getDistrict_id());
            object.put("mobile",mPhoneEdit.getText().toString().trim());
            object.put("receive_name",mReceiveNameEdit.getText().toString().trim());
            object.put("address",mLongAddressEdit.getText().toString().trim());
        } catch (JSONException e) {
            return;
        }
        StringBuilder builder=new StringBuilder("?g=Api&m=Weuser&a=address");
        builder.append("&token="+getMainApplication().getToken());
        builder.append("&address_id="+mAddress.getAddress_id());
        HttpTool.putJson(this, builder.toString(), object, new JsonHttpHandler<Void>() {
            @Override
            public void onStart() {
                super.onStart();
                mLoad.startLoad();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                mLoad.finishLoad();
            }

            @Override
            public Void parseData(JSONObject object) throws Throwable {
                return null;
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Void response) {
                ToastTool.showToast("修改成功");
                setResult(RESULT_OK);
                finish();
            }
        });
    }
}
